package com.thunder.soft.common.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.gyf.immersionbar.ImmersionBar;
import com.thunder.soft.common.util.ActivityManagerUtils;

public abstract class BaseActivity<VDB extends ViewDataBinding> extends AppCompatActivity {
    // 声明变量binding
    protected VDB binding;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 把 activity 加入栈
        ActivityManagerUtils.getInstance().push(this);
        // 初始化变量
        binding = DataBindingUtil.setContentView(this, layoutId());
        // 设置状态栏
        setImmersionBar();
        // 初始化逻辑方法
        initData();
    }

    /**
     * 设置标题栏
     * @param view 标题栏 view
     */
    protected void setTitleBar(View view) {
        ImmersionBar.setTitleBar(this, view);
    }

    /**
     * 设置状态栏 默认沉浸式 ，状态栏背景透明，字体颜色黑色
     */
    protected void setImmersionBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .setOnKeyboardListener(this::onKeyboardChange)
                .init();
    }

    /**
     * 软键盘改变时调用此方法
     *
     * @param isPopup        是否弹出
     * @param keyboardHeight 软键盘高度
     */
    protected void onKeyboardChange(boolean isPopup, int keyboardHeight) {

    }


    protected abstract void initData();

    /**
     * 布局文件
     *
     * @return 返回布局文件id
     */
    @LayoutRes
    protected abstract int layoutId();


    @Override
    protected void onDestroy() {
        if (binding != null) {
            // 解除绑定布局
            binding.unbind();
            binding = null;
        }
        // 把 activity 移除栈
        ActivityManagerUtils.getInstance().pop();
        super.onDestroy();
    }


    /**
     *
     * @param zlass
     */
    protected void startActivity(Class<? extends Activity> zlass) {
        startActivity(zlass,null);
    }

    protected void startActivity(Class<? extends Activity> zlass,Bundle bundle) {
        Intent intent = new Intent(this, zlass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
}
